package org.hydrakyoufeng.dm.toolkit;

import javax.servlet.Filter;

import org.hydrakyoufeng.j2ee.EncodingFilter;
import org.hydrakyoufeng.j2ee.ForbidFilter;
import org.hydrakyoufeng.j2ee.RequestAdapter;
import org.hydrakyoufeng.j2ee.RequestHandler;
import org.hydrakyoufeng.lang.ParameterConfig;
import org.hydrakyoufeng.tool.CastSuppoter;
import org.hydrakyoufeng.tool.function.AggregateToolkit;
import org.hydrakyoufeng.tool.json.JsonReader;
import org.hydrakyoufeng.tool.json.JsonSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig {

	private JsonReader jsonReader = JsonReader.DEFAULT;
	
	private JsonSerializer jsonSerializer = JsonSerializer.DEFAULT;
	
	@Value("${myconfig.staticPath}")
	private String staticPath;
	

	public MyConfig() {
//		MapUtil.MIN_ROWS = 6;
		
		// 系统中用到的数据转换定义
		CastSuppoter cs = CastSuppoter.DEFAULT_CAST_SUPPOTER;
		cs.setDatetimeFormat("yyyy-MM-dd HH:mm:ss");
		// 这个悲剧了……没有发现的BUG，date的默认格式本来应该是yyyy-MM-dd的，
		// 结果前端已经用yyyyMMddHHmmss进行调试了，只有将就了
		cs.setDateFormat("yyyyMMddHHmmss");
		
		ParameterConfig config = new ParameterConfig();
		config.setEmptyToNull(true);
		config.setNumToStr(true);
		config.setTrimString(true);
		jsonReader.setParameterConfig(config);
	}

	
	@Bean
	public JsonReader jsonReader() {
		return jsonReader;
	}
	
	@Bean
	public JsonSerializer jsonSerializer() {
		return jsonSerializer;
	}

	/**
	 * 编码过滤器
	 * 
	 * @return
	 */
	@Bean
	public FilterRegistrationBean<Filter> encodingFilter() {
		EncodingFilter filter = new EncodingFilter();
		filter.setEncoding("UTF-8");
		FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
		registration.addUrlPatterns("*");
		registration.setFilter(filter);
		registration.setOrder(1);
		return registration;
	}

	/**
	 * 跨域
	 * 
	 * @return
	 */
	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
		corsConfiguration.addAllowedHeader("*"); // 2允许任何头
		corsConfiguration.addAllowedMethod("*"); // 3允许任何方法（post、get等）

		source.registerCorsConfiguration("/**", corsConfiguration); // 4
		return new CorsFilter(source);
	}

	/**
	 * 框架中的用户请求参数解析器
	 * @return
	 */
	@Bean
	public RequestHandler requestHandler() {
		RequestAdapter adapter = new RequestAdapter();
		adapter.getRequestParameterResolver().setJsonReader(jsonReader);
		return adapter;
	}

	/**
	 * 配置硬盘上的图片上传映射地址
	 * 
	 * @return
	 */
	@Bean
	public WebMvcConfigurer uploadImage() {
		WebMvcConfigurer wmc = new WebMvcConfigurer() {
			@Override
			public void addResourceHandlers(ResourceHandlerRegistry registry) {
//				因为静态页面交给nginx去代理，所以这里不配置视频和图片的映射地址
//				registry.addResourceHandler("/uploadImages/**").addResourceLocations("file:" + uploadImagePath);
//				registry.addResourceHandler("/uploadVideos/**").addResourceLocations("file:" + uploadVideoPath);
				registry.addResourceHandler("/static/**").addResourceLocations("file:" + staticPath);
			}
		};
		return wmc;
	}

	@Bean
	public FilterRegistrationBean<ForbidFilter> forbidFilter() {
		ForbidFilter filter = new ForbidFilter();
		FilterRegistrationBean<ForbidFilter> registration = new FilterRegistrationBean<>();
		registration.addUrlPatterns("*.properties");
		registration.setFilter(filter);
		registration.setOrder(2);
		return registration;
	}
	
	@Bean
	public AggregateToolkit aggregateToolkit() {
		return new AggregateToolkit();
	}
	
}
